package d512a.TAKO;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.google.zxing.client.android.CaptureActivity;

import d512a.TAKO.helpers.DataBaseHelper;
import d512a.TAKO.helpers.DateHelper;

public class ProductInfoDetailedActivity extends Activity {

	private DataBaseHelper foodDB;
	private TextView txtProductName;
	private TextView txtEnergiValueDetailed;
	private TextView txtKcalValue;
	private TextView txtProteinTotalValue;
	private TextView txtTotal_NValue;
	private TextView txtFedtTotalValueDetailed;
	private TextView txtMaettede_fedtsyrerValue;
	private TextView txtMonoumaett_fedtsyrerValue;
	private TextView txtPolyumaett_fedtsyrerValue;
	private TextView txtKulhydratTotalValue;
	private TextView txtKulhydrat_tilgaengeligValue;
	private TextView txtTilsat_sukkerValue;
	private TextView txtKostfibreValue;
	private TextView txtAlkoholValue;
	private TextView txtAskeValue;
	private TextView txtVandValue;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_info_detailed);

		handleDatabase();
		addWidgetReferences();
		setWidgetListeners();
		String foodId = getIntent().getExtras().getString("foodId");
		getProductName(foodId);
		getDetailedNutrients(foodId);
		String date = DateHelper.getDate();
		
		if (ProductTabWidgetActivity.fromScan) {
		    String scanningsID = getIntent().getExtras().getString("scanningsID");
		    
            foodDB.insertInRecommended(0, null, scanningsID, 6, date).close();
        } else {
            // Get lastly inserted _id in the Recommended table
            Cursor c = foodDB.getRecommendedPrimKey_id();
            c.moveToFirst();
            String recommend_idPrimKey = c.getString(0);
            c.close();
            foodDB.insertInRecommended(0, recommend_idPrimKey, null, 6, date);
        }
	}

	private void handleDatabase() {
		foodDB = new DataBaseHelper(this);

		try {
			foodDB.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		} finally {
			foodDB.close();
		}
	}

	private void addWidgetReferences() {
		txtProductName = (TextView) findViewById(R.id.txtProductName);
		txtEnergiValueDetailed = (TextView) findViewById(R.id.txtEnergiValueDetailed);
		txtKcalValue = (TextView) findViewById(R.id.txtKcalValue);
		txtProteinTotalValue = (TextView) findViewById(R.id.txtProteinTotalValue);
		txtTotal_NValue = (TextView) findViewById(R.id.txtTotal_NValue);
		txtFedtTotalValueDetailed = (TextView) findViewById(R.id.txtFedtTotalValueDetailed);
		txtMaettede_fedtsyrerValue = (TextView) findViewById(R.id.txtMaettede_fedtsyrerValue);
		txtMonoumaett_fedtsyrerValue = (TextView) findViewById(R.id.txtMonoumaett_fedtsyrerValue);
		txtPolyumaett_fedtsyrerValue = (TextView) findViewById(R.id.txtPolyumaett_fedtsyrerValue);
		txtKulhydratTotalValue = (TextView) findViewById(R.id.txtKulhydratTotalValue);
		txtKulhydrat_tilgaengeligValue = (TextView) findViewById(R.id.txtKulhydrat_tilgaengeligValue);
		txtTilsat_sukkerValue = (TextView) findViewById(R.id.txtTilsat_sukkerValue);
		txtKostfibreValue = (TextView) findViewById(R.id.txtKostfibreValue);
		txtAlkoholValue = (TextView) findViewById(R.id.txtAlkoholValue);
		txtAskeValue = (TextView) findViewById(R.id.txtAskeValue);
		txtVandValue = (TextView) findViewById(R.id.txtVandValue);
	}

	private void setWidgetListeners() {

	}

	private void getProductName(String foodId) {
		Cursor cursor = foodDB.getProductNameFromFoodId(foodId);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			txtProductName.setText(cursor.getString(0));
		}
	}

	private void getDetailedNutrients(String foodId) {
		Cursor cursor = foodDB.getDetailedNutrientsFromFoodId(foodId);
		if (cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				int compId = Integer.parseInt(cursor.getString(0));
			    switch(compId) {
			    case 0:
                    // Might throw a NumberFormat Exception, but all BestLoc values
                    // for Energi are whole integer numbers
			    	int energiValue = Integer.parseInt(cursor.getString(1));
                    int kcal = energiValue * 1000 / 4184;   // 1 kilocalorie = 4184 joules
                    txtEnergiValueDetailed.setText(energiValue + " kJ");
                    txtKcalValue.setText(kcal + " kcal");
                    break;
			    case 1:
                    txtProteinTotalValue.setText(cursor.getString(1) + " g");
                    break;
			    case 2:
                    txtTotal_NValue.setText(cursor.getString(1) + " g");
                    break;
			    case 3:
                    txtFedtTotalValueDetailed.setText(cursor.getString(1) + " g");
                    break;
			    case 4:
                    txtMaettede_fedtsyrerValue.setText(cursor.getString(1) + " g");
                    break;
			    case 5:
                    txtMonoumaett_fedtsyrerValue.setText(cursor.getString(1) + " g");
                    break;
			    case 6:
                    txtPolyumaett_fedtsyrerValue.setText(cursor.getString(1) + " g");
                    break;
			    case 7:
                    txtKulhydrat_tilgaengeligValue.setText(cursor.getString(1) + " g");
                    break;
			    case 8:
                    txtKulhydratTotalValue.setText(cursor.getString(1) + " g");
                    break;
			    case 9:
                    txtTilsat_sukkerValue.setText(cursor.getString(1) + " g");
                    break;
			    case 10:
                    txtKostfibreValue.setText(cursor.getString(1) + " g");
                    break;
			    case 11:
                    txtAlkoholValue.setText(cursor.getString(1) + " g");
                    break;
			    case 12:
                    txtAskeValue.setText(cursor.getString(1) + " g");
                    break;
			    case 13:
                    txtVandValue.setText(cursor.getString(1) + " g");
                    break;
			    default:
                    throw new Error("Fejl 40!");
			    }
			}
		}
		cursor.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.product_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		Intent i;
		switch (item.getItemId()) {
		case R.id.menu_item_scan:
			startBarcodeScanner();
			return true;
		case R.id.menu_item_home:
			i = new Intent(getApplicationContext(), HomescreenActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_shoppingBasket:
			i = new Intent(getApplicationContext(), BasketActivity.class);
			startActivity(i);
			return true;
		case R.id.menu_item_settings:
			i = new Intent(getApplicationContext(), UserInfoActivity.class);
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void startBarcodeScanner() {
		Intent i = new Intent(getApplicationContext(), CaptureActivity.class);
		startActivity(i);
	}
}
